package part.c;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import client.ClientConnection;

public class EchoServer {

	public static void main(String[] args) {
		try {
			ServerSocket socket = new ServerSocket(6969);

			/*
			 * newCachedThreadPool can support numerous clients, I've tested it
			 * for 3, but I'm sure it could handle more. Messages all received
			 * by server when they're sent, not much waiting at all. *
			 */
			// ExecutorService executor = Executors.newCachedThreadPool();

			/*
			 * newFixedThreadPool(1), whomever connects first can send messages
			 * first. Others can also send messages, but these will not be
			 * echoed by the server until the client in front of them has quit.
			 */
			// ExecutorService executor = Executors.newFixedThreadPool(1);

			ExecutorService executor = Executors
					.newSingleThreadExecutor();

			while (true) {
				Socket client = socket.accept();
				
				System.out.println("Awaiting connections...");
				executor.execute(new ClientConnection(client));
				System.out.println("Connection accepted");
			}

		} catch (IOException ioe) {
			System.err.println(ioe);
		}
	}
}
